package com.example.rule.toolkit;

import com.example.rule.common.enums.OperatorEnum;

public class ExpressionUtils {

    private static final String DATA_FIELD = "data";

    /**
     * 构建表达式字符串
     *
     * @param field 字段名，如 "data"
     * @param operator 中文操作符，如 "大于"
     * @param value 值，如 10.0
     * @return 表达式字符串，如 "data > 10.0"
     */
    public static String buildExpression(String field, String operator, Double value) {
        String symbol = OperatorEnum.getSymbolByDescription(operator);
        return String.format("%s %s %f", field, symbol, value);
    }

    public static String buildExpression(String operator, Double value) {
        return buildExpression(DATA_FIELD, operator, value);
    }


    /**
     * 构建字符串类型的表达式（如包含、不包含）
     */
    public static String buildStringExpression(String field, String operator, String value) {
        String symbol = OperatorEnum.getSymbolByDescription(operator);
        if ("contains".equals(symbol)) {
            return String.format("%s %s \"%s\"", field, symbol, value);
        } else if ("!contains".equals(symbol)) {
            return String.format("!%s.contains(\"%s\")", field, value);
        } else {
            throw new IllegalArgumentException("不支持的字符串操作符: " + operator);
        }
    }
}
